<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Common\Model\CaozuologModel;
class ArtController extends AdminController{
	public function _initialize(){
		parent::_initialize();
	}
	/**
	 * 获取所有的文章分类
	 */
	public function getAllArticleType(){
		$type = M('article_type')->select();
		$this->ajaxReturn(['status'=>1,'data'=>$type]);
	}
	/**
	 * 文章详情
	 */
	public function details(){
		$id=I('id');
		$art=M('article')->where('id ='.$id)->find();
		$data['list']=$art;
		$data['status']=1;
		$this->ajaxReturn($data);
	}
	/**
	 * 新增文章
	 */
	public function addArticle(){
		$title = I('title');
		$status = I('status');
		$content = $_POST['content'];
		if(empty($title)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少标题'
					];
			$this->ajaxReturn($data);
		}
		if(empty($status)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少状态'
					];
			$this->ajaxReturn($data);
		}
		if(empty($content)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少内容'
					];
			$this->ajaxReturn($data);
		}
		$insert = [
			'title'=>$title,
			'content'=>$content,
			'status'=>$status,
			'add_time'=>time(),
		];
		$r = M('article')->add($insert);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'新增成功'
				];
		$M_LOG = new CaozuologModel();
		$content = '添加文章，id为'.$r;
		$M_LOG->addLog($this->user['username'], $content);
		$this->ajaxReturn($data);
	}
	/**
	 * 修改文章
	 */
	public function changeArticle(){
		$id=I('id');
		$title = I('title');
		$status = I('status');
		$content = $_POST['content'];
		if(empty($title)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少标题'
					];
			$this->ajaxReturn($data);
		}
		if(empty($status)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少状态'
					];
			$this->ajaxReturn($data);
		}
		if(empty($content)){
			$data = [
			'status'=>-1,
			'msg'=>'缺少内容'
					];
			$this->ajaxReturn($data);
		}		
		$insert = [
			'title'=>$title,
			'content'=>$content,
			'status'=>$status,
			'type'=>$type,
		];
		$r = M('article')->where('id ='.$id)->save($insert);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'修改成功'
				];
		$M_LOG = new CaozuologModel();
		$content = '修改文章，id为'.$id;
		$M_LOG->addLog($this->user['username'], $content);
		$this->ajaxReturn($data);
	}
	/**
	 * 删除文章
	 */
	public function disableArticle(){
		$id = I('id');
		if(empty($id)){
			$return = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($return);
		}
		$article=M('article')->where(['id'=>$id])->find();
		if(!$article||$article['status']==-1){
			$return = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($return);
		}
		$r = M('article')->where('id ='.$id)->setField('status',-1);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'禁用成功'
				];
		$M_LOG = new CaozuologModel();
		$content = '禁用文章，id为'.$id;
		$M_LOG->addLog($this->user['username'], $content);
		$this->ajaxReturn($data);
	}
	/**
	 * 开启文章
	 */
	public function enableArticle(){
		$id = I('id');
		if(empty($id)){
			$return = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($return);
		}
		$article=M('article')->where(['id'=>$id])->find();
		if(!$article||$article['status']==1){
			$return = [
			'status'=>-1,
			'msg'=>'参数错误'
					];
			$this->ajaxReturn($return);
		}
		$r = M('article')->where('id ='.$id)->setField('status',1);
		if(!$r){
			$data = [
			'status'=>-1,
			'msg'=>'服务器繁忙'
					];
			$this->ajaxReturn($data);
		}
		$data = [
		'status'=>1,
		'msg'=>'启用成功'
				];
		$M_LOG = new CaozuologModel();
		$content = '启用文章，id为'.$id;
		$M_LOG->addLog($this->user['username'], $content);
		$this->ajaxReturn($data);
	}
	/**
	 * 获取文章
	 */
	public function getAllArticle(){
		$order = '';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
					'status'=>-1,
					'msg'=>'参数错误，排序参数错误'
				];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'a.add_time':$order = 'a.add_time desc';
			
		}
		$id = I('id');
		$title = I('title');
		$begin = I('begin');
		$last = I('last');
		$page = I('page');
		$num = I('num');
		$status=I('status');
		if(!empty($id)){
			$where['a.id'] = $id;
		}
		if(!empty($title)){
			$where['a.title'] = ['like','%'.$title.'%'];
		}
		if(!empty($status)){
			$where['a.status'] = $status;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['a.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$pages = $this->DoPage($page, $num);
		$r = M('article')
			->alias('a')
			->where($where)
			->limit($pages['limit'],$pages['num'])
			->order($order)
			->select();
		$count = M('article')
			->alias('a')
			->where($where)
			->count();
		$data = [
			'data'=>$r,
			'status'=>1,
			'count'=>$count,
		];
		$this->ajaxReturn($data);
	}
	/**
	 * 文章详情
	 */
	public function noticeDetails(){
	    $id=I('id');
	    $art=M('Notice')->where('id ='.$id)->find();
	    $data['list']=$art;
	    $data['status']=1;
	    $this->ajaxReturn($data);
	}
	/**
	 * 新增公告
	 */
	public function addNotice(){
	    $title = I('title');
	    $status = I('status');
	    $content = I('content');
	    if(empty($title)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'缺少标题'
	        ];
	        $this->ajaxReturn($data);
	    }
	    if(empty($status)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'缺少状态'
	        ];
	        $this->ajaxReturn($data);
	    }
	    if(empty($content)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'缺少内容'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $type = I('type');
	    if(empty($type)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'请选择文章类型'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $insert = [
	        'title'=>$title,
	        'content'=>$content,
	        'status'=>$status,
	        'type'=>$type,
	        'add_time'=>time(),
	    ];
	    $r = M('Notice')->add($insert);
	    if(!$r){
	        $data = [
	            'status'=>-1,
	            'msg'=>'服务器繁忙'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $data = [
	        'status'=>1,
	        'msg'=>'新增成功'
	    ];
	    $M_LOG = new CaozuologModel();
	    $content = '添加公告，id为'.$r;
	    $M_LOG->addLog($this->user['username'], $content);
	    $this->ajaxReturn($data);
	}
	/**
	 * 修改文章
	 */
	public function changeNotice(){
	    $id=I('id');
	    $title = I('title');
	    $status = I('status');
	    $content = I('content');
	    if(empty($title)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'缺少标题'
	        ];
	        $this->ajaxReturn($data);
	    }
	    if(empty($status)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'缺少状态'
	        ];
	        $this->ajaxReturn($data);
	    }
	    if(empty($content)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'缺少内容'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $type = I('type');
	    if(empty($type)){
	        $data = [
	            'status'=>-1,
	            'msg'=>'请选择文章类型'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $insert = [
	        'title'=>$title,
	        'content'=>$content,
	        'status'=>$status,
	        'type'=>$type,
	    ];
	    $r = M('Notice')->where('id ='.$id)->save($insert);
	    if(!$r){
	        $data = [
	            'status'=>-1,
	            'msg'=>'服务器繁忙'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $data = [
	        'status'=>1,
	        'msg'=>'修改成功'
	    ];
	    $M_LOG = new CaozuologModel();
	    $content = '修改公告，id为'.$id;
	    $M_LOG->addLog($this->user['username'], $content);
	    $this->ajaxReturn($data);
	}
	/**
	 * 删除公告
	 */
	public function disableNotice(){
	    $id = I('id');
	    if(empty($id)){
	        $return = [
	            'status'=>-1,
	            'msg'=>'参数错误'
	        ];
	        $this->ajaxReturn($return);
	    }
	    $article=M('Notice')->where(['id'=>$id])->find();
	    if(!$article||$article['status']==-1){
	        $return = [
	            'status'=>-1,
	            'msg'=>'参数错误'
	        ];
	        $this->ajaxReturn($return);
	    }
	    $r = M('Notice')->where('id ='.$id)->setField('status',-1);
	    if(!$r){
	        $data = [
	            'status'=>-1,
	            'msg'=>'服务器繁忙'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $data = [
	        'status'=>1,
	        'msg'=>'禁用成功'
	    ];
	    $M_LOG = new CaozuologModel();
	    $content = '禁用公告，id为'.$id;
	    $M_LOG->addLog($this->user['username'], $content);
	    $this->ajaxReturn($data);
	}
	/**
	 * 开启公告
	 */
	public function enableNotice(){
	    $id = I('id');
	    if(empty($id)){
	        $return = [
	            'status'=>-1,
	            'msg'=>'参数错误'
	        ];
	        $this->ajaxReturn($return);
	    }
	    $article=M('Notice')->where(['id'=>$id])->find();
	    if(!$article||$article['status']==1){
	        $return = [
	            'status'=>-1,
	            'msg'=>'参数错误'
	        ];
	        $this->ajaxReturn($return);
	    }
	    $r = M('article')->where('id ='.$id)->setField('status',1);
	    if(!$r){
	        $data = [
	            'status'=>-1,
	            'msg'=>'服务器繁忙'
	        ];
	        $this->ajaxReturn($data);
	    }
	    $data = [
	        'status'=>1,
	        'msg'=>'启用成功'
	    ];
	    $M_LOG = new CaozuologModel();
	    $content = '启用公告，id为'.$id;
	    $M_LOG->addLog($this->user['username'], $content);
	    $this->ajaxReturn($data);
	}
	/**
	 * 获取文章
	 */
	public function getAllNotice(){
	    $order = '';
	    $sort = I('sort');
	    if(!empty($sort)){
	        if(!in_array($sort, [1,2])){
	            $data = [
	                'status'=>-1,
	                'msg'=>'参数错误，排序参数错误'
	            ];
	            $this->ajaxReturn($data);
	        }
	        $sort==1?$order = 'a.add_time':$order = 'a.add_time desc';
	        	
	    }
	    $id = I('id');
	    $title = I('title');
	    $begin = I('begin');
	    $last = I('last');
	    $page = I('page');
	    $num = I('num');
	    $status=I('status');
	    if(!empty($id)){
	        $where['a.id'] = $id;
	    }
	    if(!empty($title)){
	        $where['a.title'] = ['like','%'.$title.'%'];
	    }
	    $type = I('type');
	    if(!empty($type)){
	        $where['a.type'] = $type;
	    }
	    if(!empty($status)){
	        $where['a.status'] = $status;
	    }
	    $whereTime = $this->dotime($begin, $last);
	    $where['a.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
	    $pages = $this->DoPage($page, $num);
	    $r = M('Notice')
	    ->alias('a')
	    ->where($where)
	    ->limit($pages['limit'],$pages['num'])
	    ->order($order)
	    ->select();
	    $count = M('Notice')
	    ->alias('a')
	    ->where($where)
	    ->count();
	    $data = [
	        'data'=>$r,
	        'status'=>1,
	        'count'=>$count,
	    ];
	    $this->ajaxReturn($data);
	}
	/**
	 * 获取留言
	 */
	public function getAllQuestion(){
	    $order = '';
	    $sort = I('sort');
	    if(!empty($sort)){
	        if(!in_array($sort, [1,2])){
	            $data = [
	                'status'=>-1,
	                'msg'=>'参数错误，排序参数错误'
	            ];
	            $this->ajaxReturn($data);
	        }
	        $sort==1?$order = 'a.add_time':$order = 'a.add_time desc';
	        	
	    }
	    $id = I('id');
	    $title = I('title');
	    $begin = I('begin');
	    $last = I('last');
	    $page = I('page');
	    $num = I('num');
	    $status=I('status');
	    if(!empty($id)){
	        $where['a.id'] = $id;
	    }
	    if(!empty($title)){
	        $where['a.title'] = ['like','%'.$title.'%'];
	    }
	    $type = I('type');
	    if(!empty($type)){
	        $where['a.type'] = $type;
	    }
	    if(!empty($status)){
	        $where['a.status'] = $status;
	    }
	    $whereTime = $this->dotime($begin, $last);
	   // $where['a.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
	    $pages = $this->DoPage($page, $num);
	    $r = M('Question')
	    ->alias('a')
	    ->field('a.*,u.username')
		->join('__MEMBER__ as u on u.member_id = a.uid')
	    ->where($where)
	    ->limit($pages['limit'],$pages['num'])
	    ->order($order)
	    ->select();
	    //echo M('Question')->_sql();
	    $count = M('Question')
	    ->where($where)
	    ->count();
	    $data = [
	        'data'=>$r,
	        'status'=>1,
	        'count'=>$count,
	    ];
	    $this->ajaxReturn($data);
	}
}